/**
 * 检查头盔分类问题脚本
 */

const mysql = require('mysql2/promise');
require('dotenv').config({ path: './nestjs-app/.env' });

async function checkHelmetClassification() {
  try {
    const connection = await mysql.createConnection({
      host: process.env.DB_HOST || '43.143.253.188',
      port: process.env.DB_PORT || 3306,
      user: process.env.DB_USER || 'nextjs',
      password: process.env.DB_PASS || 'yEAiRE2jcxTyRBy5',
      database: process.env.DB_NAME || 'nextjs',
      timezone: '+08:00'
    });

    console.log('🔍 检查头盔分类问题...');
    
    // 检查所有头盔的基础类型分布
    const [baseStats] = await connection.execute(`
      SELECT base, COUNT(*) as count
      FROM equipment_base 
      WHERE type = 1
      GROUP BY base 
      ORDER BY base
    `);

    console.log('📊 头盔基础类型分布:');
    baseStats.forEach(stat => {
      const baseName = getBaseName(stat.base);
      console.log(`  基础类型 ${stat.base} (${baseName}): ${stat.count} 个头盔`);
    });

    // 检查武士头盔详细列表
    console.log('\n📋 武士头盔详细列表:');
    const [warriorHelmets] = await connection.execute(`
      SELECT id, name, type, base, level 
      FROM equipment_base 
      WHERE type = 1 AND base = 1
      ORDER BY level, id
    `);

    warriorHelmets.forEach(helmet => {
      console.log(`  ID=${helmet.id}, 名称=${helmet.name}, 等级=${helmet.level}`);
    });

    // 分析分类问题
    console.log('\n🔍 分析分类问题:');
    
    // 1. 检查长冠服和冕冠服 - 这些应该是防具，不是头盔
    const crownHelmets = warriorHelmets.filter(helmet => 
      helmet.name.includes('长冠服') || helmet.name.includes('冕冠服')
    );

    if (crownHelmets.length > 0) {
      console.log('❌ 长冠服和冕冠服分类错误 (应该是防具，不是头盔):');
      crownHelmets.forEach(helmet => {
        console.log(`  ID=${helmet.id}, 名称=${helmet.name} - 这些应该是防具，不是头盔`);
      });
    }

    // 2. 检查通用头盔 - 布帽、方士帽、貂皮帽等
    const genericHelmets = warriorHelmets.filter(helmet => 
      helmet.name === '布帽' || 
      helmet.name === '方士帽' || 
      helmet.name === '貂皮帽'
    );

    if (genericHelmets.length > 0) {
      console.log('⚠️  通用头盔 (可能需要重新分类):');
      genericHelmets.forEach(helmet => {
        console.log(`  ID=${helmet.id}, 名称=${helmet.name} - 通用头盔，默认为武士`);
      });
    }

    // 3. 检查纯武士头盔
    const pureWarriorHelmets = warriorHelmets.filter(helmet => 
      helmet.name.includes('·武')
    );

    console.log(`\n✅ 纯武士头盔数量: ${pureWarriorHelmets.length}`);
    pureWarriorHelmets.forEach(helmet => {
      console.log(`  ID=${helmet.id}, 名称=${helmet.name}, 等级=${helmet.level}`);
    });

    // 4. 检查其他职业的头盔数量
    console.log('\n📊 其他职业头盔数量:');
    for (let base = 2; base <= 3; base++) {
      const [helmets] = await connection.execute(`
        SELECT COUNT(*) as count
        FROM equipment_base 
        WHERE type = 1 AND base = ?
      `, [base]);
      
      const baseName = getBaseName(base);
      console.log(`  基础类型 ${base} (${baseName}): ${helmets[0].count} 个头盔`);
    }

    await connection.end();
  } catch (error) {
    console.error('❌ 检查失败:', error.message);
  }
}

function getBaseName(base) {
  const baseNames = {
    1: '武士',
    2: '文人',
    3: '异师'
  };
  return baseNames[base] || '未知';
}

checkHelmetClassification();
